<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript字符串及其方法</title>
</head>
<body>
    <h1>JavaScript字符串及其方法</h1>
    <script>
        let str1 = '单引号定义';
        document.writeln(str1 + "<br>")
        let str2 = "双引号定义"
        document.writeln(str2 + "<br>")
        let str3 = "\"转义符号\\\"";
        document.writeln(str3 + "<br>")
        let str4 = "字符串连接符+" + "def";
        document.writeln(str4 + "<br>")
        let str5 = "\xA9";
        document.writeln(str5 + "<br>")
        let str6 = "\u00A9";  //UTF-8字符串定义
        document.writeln(str6 + "<br>")

        let str7 = new String("字符串对象String");
        document.writeln(str7 + "<br>");

        //字符串方法
        console.log("12,34,56".split(","));
        document.writeln("abc中国def".indexOf("中"));
        document.write("<br>");
        document.writeln("字符串拼接".concat(str7));
        document.write("<br>");

        //数值  <-->  字符串
        let n = 3;
        document.writeln(typeof n.toString());
        document.write("<br>");

        document.writeln(parseFloat("12.3a"));
        document.write("<br>")
        document.writeln(parseFloat("abc"));
        document.write("<br>")
        document.writeln(parseFloat("1abc"));
        document.write("<br>")
        document.writeln(parseFloat("1.01abc"));
        document.write("<br>")
        document.writeln(parseInt("1abc"));
        document.write("<br>")

        //模板字符串  反引号``  支持${} 解析
        let name = "张三";
        let age = 19
        document.writeln("姓名：" + name + " , " + "年龄：" + age);
        document.write("<br>")
        document.writeln(`姓名：${name} , 年龄：${age}`);
        document.write("<br>")

        let str8 = `
            多行文本1
            多行文本2
        `;
        document.writeln(str8 + "<br>");
        console.log(str8);

        //字符串长度以及数组的形式访问
        let a = "中123";
        document.writeln(a.length + "<br>");
        document.writeln(a[0] + "<br>");
        document.writeln(a[1] + "<br>");
        document.writeln(a[2] + "<br>");
        document.writeln(a[3] + "<br>");

    </script>
</body>
</html>